/** @file    BigBrotherImpl.cpp
 *  @author  Alessandro Polo
 *  @version $Id: BigBrotherEvaluation.cpp 2821 2010-08-01 11:44:26Z alex $
 *  @brief
 * File containing methods for the BigBrotherImpl class.
 * The header for this class can be found in BigBrotherImpl.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "BigBrotherImpl.h"

 #include <core/Message.h>
 #include <core/Request.h>
 #include <core/Response.h>
 #include <core/Fact.h>
 #include <core/MethodsCommon.h>
 #include <core/UserManager.h>

 #include <interfaces/services/BuildingManager.h>

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void BigBrotherImpl::recognizeUser_( bool userFound, Location* cLocation, Message* message )
 {
	if ( cLocation == NULL || message == NULL ) return;
	int usersInHome = UserManager::getUserCountAtHome();
	Log(LOG_VERBOSE, ":recognizeUser_(%d,%s) : by %s [UsersAtHome=%d]", userFound, cLocation->getName().c_str(), message->getSource().toString().c_str(), usersInHome );

	if ( usersInHome == 0 ) {
		// no users were at home!
		// hopefully an authorized user is entering the house
		bool done;
		if ( userFound )
			done = recognizeUserAppearedIn_(cLocation, message );
		else
			done = recognizeUserDisappearedIn_(cLocation, message );
		if ( done )
			return;
	 }
	else if ( usersInHome > 1 ) {
		// BAD i cant manage multiple users
	 }

	if ( cLocation->getUsers().empty() ) {
		// no users were there, so:
		//  - we were wrong (user was already there)
		//  - user has come into the zone from another zone or a parent zone

		Log(LOG_VERBOSE, ":recognizeUser_(%d,%s) : Previously empty [UsersAtHome=%d]", userFound, cLocation->getName().c_str(), usersInHome );

		bool done;
		if ( userFound )
			done = recognizeUserAppearedIn_(cLocation, message );
		else
			done = recognizeUserDisappearedIn_(cLocation, message );

		if ( done )
			return;

	 }
	else if ( cLocation->getUsers().size() == 1 ) {

		Log(LOG_VERBOSE, ":recognizeUser_(%d,%s) : %s was there [UsersAtHome=%d]", userFound, cLocation->getName().c_str(), cLocation->getUser(0).c_str(), usersInHome );

		if (  usersInHome == 1 ) {
			// probably it's the (same) current user, confirmed by the event
			//BAD: MAY BE AN ALARM!!!!!
			setUserLocation_(cLocation->getUser(0), cLocation, 1.0 );
		 }
		else {
			//BAD: MULTIPLE USER NOT MANAGED
		 }
	 }
	else { // multiple (previous) users in location
		//BAD: MULTIPLE USER NOT MANAGED
	 }

 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool BigBrotherImpl::recognizeUserAppearedIn_( Location* cLocation, Message* )
 {
	if ( cLocation == NULL ) return false;
	map<string, double> accessLocs;
	Location* parentLocation = getLocationParent_(cLocation);

	if ( parentLocation != NULL ) {
		// location has a parent, maybe an user moved inside the parent zone
		// entering this sub-zone
		if ( parentLocation->getUsers().size() == 1 ) {
			///BAD: we are guessing..
			setUserLocation_(parentLocation->getUser(0), cLocation, 0.6 );
			return true;
		 }

		if ( cLocation->getLocationType() == Location::TYPE_DEVICE ) {
			if ( parentLocation->hasAccessToLocations() )
				accessLocs = parentLocation->getAccessTo();
		 }

	 }
	else {
		// no parent.. 
	 }

//	if ( cLocation->hasAccessToLocations() )
//		accessLocs.push_back( cLocation->getAccessTo().at(0) );

	// maybe an user entered the zone by an accessible room
/*
	if ( accessLocs.size() >= 1 ) {
		Location* accessLocation = this->locations.key_( accessLocs.at(0) );
		if ( accessLocation != NULL && accessLocation->getUsers().size() == 1 ) {
			setUserLocation_(accessLocation->getUser(0), cLocation, 0.4 );
			return true;
		 }
	 }
*/
	return false;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool BigBrotherImpl::recognizeUserDisappearedIn_( Location* cLocation, Message* )
 {
	if ( cLocation == NULL ) return false;

	if ( cLocation->getUsers().size() == 1 ) {
		///BAD: we are guessing..
		setUserLocation_(cLocation->getUser(0), cLocation, 0.0 );
		return true;
	 }

	return false;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////


